package com.mf.service.impl.basic.webPage;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.mf.entity.dto.basic.webPage.DtoPageItem;
import com.mf.entity.req.basic.search.webPage.SearchReqPageItem;
import com.mf.mapper.basic.webPage.MapperPageItem;
import com.mf.service.basic.webPage.ServicePageItem;

import java.util.List;


@Service
public class ImplServicePageItem extends ServiceImpl<MapperPageItem, DtoPageItem> implements ServicePageItem {
    @Resource
    private MapperPageItem mapperPageItem;

    /**
     * 参数查询条件
     */
    @Override
    public LambdaQueryWrapper<DtoPageItem> fillParams(SearchReqPageItem params) {
        LambdaQueryWrapper<DtoPageItem> wr = loadQw();
        wr.eq(ObjectUtils.isNotEmpty(params.getPageId()), DtoPageItem::getPageId, params.getPageId())
                .eq(ObjectUtils.isNotEmpty(params.getJsonId()), DtoPageItem::getJsonId, params.getJsonId())
                .eq(StringUtils.isNotBlank(params.getType()), DtoPageItem::getType, params.getType())
                .eq(ObjectUtils.isNotEmpty(params.getIsMainTable()), DtoPageItem::getIsMainTable,
                        params.getIsMainTable())
                .orderByDesc(DtoPageItem::getSort).orderByDesc(DtoPageItem::getId);
        return wr;
    }

    /**
     * 根据jsonId查询使用数量
     */
    @Override
    public long countByJsonId(Integer jsonId) {
        LambdaQueryWrapper<DtoPageItem> w = loadQw();
        w.eq(DtoPageItem::getJsonId, jsonId);
        return count(w);
    }

    /**
     * 根据页面id查询数量
     */
    @Override
    public long countByPageId(Integer pageId) {
        LambdaQueryWrapper<DtoPageItem> wr = loadQw();
        wr.eq(DtoPageItem::getPageId, pageId);
        return count(wr);
    }

    /**
     * 删除
     */
    @Override
    public void deleteById(Integer pageId, Integer id) {
        LambdaQueryWrapper<DtoPageItem> wrapper = loadQw();
        wrapper.eq(DtoPageItem::getPageId, pageId).eq(DtoPageItem::getId, id);
        remove(wrapper);
    }

    /**
     * 根据页面id查询
     */
    @Override
    public List<DtoPageItem> findPageItemByPageId(Integer id) {
        return mapperPageItem.findPageItemByPageId(id);
    }


}